import { Request, Response, NextFunction} from 'express';
import _ from 'lodash';
import { getTutor, createTutor, updateTutor, deleteTutor, getIdTutor, tutorStatusChange } from './tutor.service';
import { getFavorite } from '../favorites/favorites.service';
import { cornerDestroyById } from '../corner/common.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  let pageNo = request.query.pageNo;
  let pageSize = request.query.pageSize;

  const data = await getTutor(parseInt(`${pageSize}`, 10), parseInt(`${pageNo}`, 10));
  
  response.send(data);
}

/**
 * 获取内容(按 ID 查找)
 */
export const tutorDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdTutor(parseInt(postId, 10));

    const user = request.user;
    if (user && user.id > 0) {
      let favoriteParam = { user_id: user.id, type: 9, target_id: data.id }
      let favorite = await getFavorite(favoriteParam);
      data.favorite = (favorite as any).length > 0 ? favorite[0] : null;
    }

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 创建内容
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容
  let tutorMdel = request.body;

  // 创建内容
  if (tutorMdel.id && tutorMdel.id > 0) {
    delete tutorMdel.total;
    const data = await updateTutor(tutorMdel, parseInt(tutorMdel.id, 10));
    response.send(data);
  } else {
    const data = await createTutor(tutorMdel);
    response.send(data);
  }
}

/**
 * 更新内容
 */
export const update = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 获取更新内容
  const post = _.pick(request.body, [
    'name_zh',
    'name_ug',
    'remark_zh',
    'remark_ug',
    'lectureRange_zh',
    'lectureRange_ug',
    'price',
    'profession',
    'graduationTime',
    'graduatedSchool',
    'education',
    'detailed_zh',
    'detailed_ug',
    'contactPerson_zh',
    'contactPerson_ug',
    'wechat',
    'phoneNum',
  ])

  // 更新内容
  try {
    const data = await updateTutor(post, parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取删除内容 ID
  const { postId } = request.body;

  // 删除内容
  try {
    const data = await deleteTutor(parseInt(postId, 10));
    
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 状态
 */
export const status = async (
  request: Request,
  response: Response,
  next: NextFunction,
) => {
  // 获取内容 ID
  const postId = request.body.id;

  // 获取内容
  const post = _.pick(request.body, ['status', 'remark_zh', 'remark_ug']);

  // 执行
  try {
    let tutor = await getIdTutor(postId);
    const data = await tutorStatusChange(parseInt(postId, 10), post);
    if (data) {
      let notice = {
        type: 2,
        target_id: (data as any).insertId,
        target_type: 9,
        content_ug: post.remark_ug,
        content_zh: post.remark_zh,
        status: 0
      }
      // await createNotification(notice, tutor.user_id)
    }
    response.send(data);
  } catch (error) {
    next(error);
  }
};

